<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="clearfix">
          <span>院系管理</span>
        </div>
      </template>

      <!-- 搜索表单 -->
      <el-form
        :model="listQuery"
        ref="queryForm"
        :inline="true"
        label-width="68px"
        class="query-form"
      >
        <el-form-item label="院系名称" prop="keyword">
          <el-input
            v-model="listQuery.keyword"
            placeholder="请输入院系名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleSearchList"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleSearchList">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="action-buttons">
        <el-button
          type="primary"
          @click="handleAddDepartment"
        >
          <el-icon><Plus /></el-icon>添加院系
        </el-button>
        <el-button @click="getList">
          <el-icon><Refresh /></el-icon>刷新
        </el-button>
      </div>

      <!-- 数据表格 -->
      <el-table
        v-loading="listLoading"
        :data="list"
        border
        style="width: 100%"
      >
        <el-table-column label="编号" width="100" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="院系名称" min-width="200">
          <template #default="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="院系代码" width="150" align="center">
          <template #default="scope">{{ scope.row.code }}</template>
        </el-table-column>
        <el-table-column label="负责人" width="120" align="center">
          <template #default="scope">{{ scope.row.head }}</template>
        </el-table-column>
        <el-table-column label="联系电话" width="150" align="center">
          <template #default="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="创建时间" width="160" align="center">
          <template #default="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status | statusFilter">
              {{ scope.row.status | statusTextFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button
              size="small"
              type="primary"
              @click="handleUpdateDepartment(scope.$index, scope.row)"
            >
              <el-icon><Edit /></el-icon>编辑
            </el-button>
            <el-button
              size="small"
              type="success"
              @click="handleViewMajors(scope.$index, scope.row)"
            >
              <el-icon><Collection /></el-icon>专业
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDeleteDepartment(scope.$index, scope.row)"
            >
              <el-icon><Delete /></el-icon>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="listQuery.page"
        v-model:limit="listQuery.size"
        @pagination="getList"
      />

      <!-- 添加院系对话框 -->
      <el-dialog
        title="添加院系"
        v-model="addDialogVisible"
        width="500px"
      >
        <el-form
          ref="addForm"
          :model="addForm"
          :rules="addRules"
          label-width="100px"
        >
          <el-form-item label="院系名称" prop="name">
            <el-input v-model="addForm.name" placeholder="请输入院系名称" />
          </el-form-item>
          <el-form-item label="院系代码" prop="code">
            <el-input v-model="addForm.code" placeholder="请输入院系代码" />
          </el-form-item>
          <el-form-item label="负责人" prop="head">
            <el-input v-model="addForm.head" placeholder="请输入负责人姓名" />
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="addForm.phone" placeholder="请输入联系电话" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="addForm.status" placeholder="请选择状态">
              <el-option label="禁用" :value="0" />
              <el-option label="启用" :value="1" />
            </el-select>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitAddForm">确 定</el-button>
          </div>
        </template>
      </el-dialog>

      <!-- 编辑院系对话框 -->
      <el-dialog
        title="编辑院系"
        v-model="editDialogVisible"
        width="500px"
      >
        <el-form
          ref="editForm"
          :model="editForm"
          :rules="editRules"
          label-width="100px"
        >
          <el-form-item label="院系名称" prop="name">
            <el-input v-model="editForm.name" placeholder="请输入院系名称" />
          </el-form-item>
          <el-form-item label="院系代码" prop="code">
            <el-input v-model="editForm.code" placeholder="请输入院系代码" />
          </el-form-item>
          <el-form-item label="负责人" prop="head">
            <el-input v-model="editForm.head" placeholder="请输入负责人姓名" />
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="editForm.phone" placeholder="请输入联系电话" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="editForm.status" placeholder="请选择状态">
              <el-option label="禁用" :value="0" />
              <el-option label="启用" :value="1" />
            </el-select>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitEditForm">确 定</el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { Plus, Edit, Refresh, Delete, Collection } from '@element-plus/icons-vue'
import { getDepartmentPage, deleteDepartment, addDepartment, getDepartmentById, updateDepartment } from '@/api/edu'

const defaultListQuery = {
  page: 1,
  size: 10,
  keyword: null
}

export default {
  name: 'DepartmentList',
  components: {
    Plus,
    Edit,
    Refresh,
    Delete,
    Collection
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'info',
        1: 'success'
      }
      return statusMap[status]
    },
    statusTextFilter(status) {
      const statusTextMap = {
        0: '禁用',
        1: '启用'
      }
      return statusTextMap[status]
    }
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: 0,
      listLoading: false,
      addDialogVisible: false,
      editDialogVisible: false,
      addForm: {
        name: '',
        code: '',
        head: '',
        phone: '',
        status: 1
      },
      editForm: {
        id: '',
        name: '',
        code: '',
        head: '',
        phone: '',
        status: 1
      },
      addRules: {
        name: [{ required: true, message: '请输入院系名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入院系代码', trigger: 'blur' }],
        head: [{ required: true, message: '请输入负责人姓名', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },
      editRules: {
        name: [{ required: true, message: '请输入院系名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入院系代码', trigger: 'blur' }],
        head: [{ required: true, message: '请输入负责人姓名', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getDepartmentPage(this.listQuery).then(response => {
        this.list = response.records
        this.total = response.total
        this.listLoading = false
      })
    },
    handleSearchList() {
      this.listQuery.page = 1
      this.getList()
    },
    resetQuery() {
      this.listQuery = Object.assign({}, defaultListQuery)
      this.getList()
    },
    handleAddDepartment() {
      this.addDialogVisible = true
      this.addForm = {
        name: '',
        code: '',
        head: '',
        phone: '',
        status: 1
      }
    },
    handleUpdateDepartment(index, row) {
      getDepartmentById(row.id).then(response => {
        this.editForm = response.data
        this.editDialogVisible = true
      })
    },
    handleViewMajors(index, row) {
      this.$router.push(`/edu/department/${row.id}/majors`)
    },
    handleDeleteDepartment(index, row) {
      this.$confirm('是否要删除该院系?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDepartment(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    },
    submitAddForm() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          addDepartment(this.addForm).then(response => {
            this.$message({
              message: '添加成功',
              type: 'success',
              duration: 1000
            })
            this.addDialogVisible = false
            this.getList()
          })
        }
      })
    },
    submitEditForm() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          updateDepartment(this.editForm).then(response => {
            this.$message({
              message: '修改成功',
              type: 'success',
              duration: 1000
            })
            this.editDialogVisible = false
            this.getList()
          })
        }
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.action-buttons {
  display: flex;
  justify-content: flex-end;
  margin-top: -10px;
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  text-align: right;
}
</style>